<?php
class Files extends Controller {

	function Files()
	{
		parent::Controller();
		$this->users->check();
		$this->load->helper('file');
		$this->load->model('dgfile');
	}

	function index()
	{
		$data['files'] = $this->dgfile->get_files();
		$template['headers'] = '<script src="'.base_url().'scripts/jquery.MultiFile.js"></script>';

		$template['scripts'] = '
		$("#fileu").MultiFile(); 
		$(".list li p a,.titlelink").click(function() { return false; });

		$(".list li p a").click(function () { 
			var title = $(this).attr("title");
	
			$.ajax({
		   type: "POST",
		   url: "' . base_url() . index_page() . '/files/delete",
		   data: "file=" + title,
		   success: function(msg){
	
				} 
			});
			$(this).parents("li").animate({ height: "hide" }, 200);
		});
	
		$(".titlelink").click(function () { 
	
			$.ajax({
		   type: "POST",
		   url: "' . base_url() . index_page() . '/files/deleteall",
		   success: function(msg){
		$("ol.list").animate({ opacity: "hide" }, 00);
				} 
			});
			
		});
		
		/*$(".submit").click(function () { 
		var fileVal = $(":file").val();
		var commentVal = $(":text").val();
		
		
			$.post("' . base_url() . index_page() . '/files/do_upload",
			{ file: fileVal, comment: commentVal },
			function(data){
				}
			);

		});*/';
	
		// If no files, display a notice
		if (empty($data['files'])) 
		{
			$data['notice'] = 'No files have been uploaded';
		}
		
		$template['content'] = $this->load->view('files', $data, TRUE);
		$template['title'] = 'Files';
		$this->load->view('template', $template);
		
	}
	function delete() {
		$this->dgfile->delete_file($_POST['file']);
	}
	function deleteall() {
		//Deletes all files
		delete_files('files');
		$this->db->empty_table('files'); 
		redirect('files');
	}
	function do_upload() 
	{
		$config['upload_path'] = './files/';
		$config['allowed_types'] = 'ppt|jpg|png|gif|psd|xcf|mov|zip|gz|bz|tar|doc|txt|rtf|php|html|xml|rb|rxml|odt|fla|py|sql|exe|msi|mp3|wmv|wma|ogg|mp4|xhtml|phps|dll|dat|csv|pdf|tar.gz|js';
		$config['max_size']	= '10000';
		$config['max_width']  = '6000';
		$config['max_height']  = '6000';
		$this->load->library('upload', $config);
		$field_name = "file";
		if ( ! $this->upload->do_upload($field_name))
		{
			$error = array('error' => $this->upload->display_errors());
		}
		else
		{
			$file = $this->upload->data();
			
			$this->db->set('comment', $_POST['comment']);
			$this->db->set('filename', $file['file_name']);
			$this->db->set('date_upload', now());
			$this->db->set('user_upload', $this->users->getInfo($this->users->user,'id'));
			$this->db->insert('files'); 
			
			redirect('files');
		}
	}
}
?> 